

"""
"""

import timer
from utils import Enumeration as Enum
from Poll import poll
from fal.event import EventManager

class Phase(object):
    
    phases = None # Defined below
    
    def __init__(self, game, **kwargs):
        """Do setup for this phase.
        
        A new Phase object is created for each phase of each turn.
        __init__ should setup the phase so that it is ready for the actions
        that will come in Phase.doAction(). Phases that require polling
        the players should do that here, and handle the responses in
        Phase.doAction(). 
        """
        super(Phase, self).__init__(self, **kwargs)
        self.game = game
    
    def doAction(self, action):
        """Process an action.
        
        Handle an action that came in to the Game class. This probably
        means modifying a list at first, then when ready, doing a bunch of
        stuff.
        """
        raise NotImplementedError
    
    def createNextPhase(self):
        index = self.phases.index(type(currPhase))
        _type = self.phases[(index + 1) % len(self.phases)]
        return _type(currPhase.game)
    

class PreGamePhase(Phase):
    
    def __init__(self, game, **kwargs):
        super(PreGamePhase, self).__init__(game, **kwargs)
        ##TODO: Pre-Game Phase Stuff.
        
    def doAction(self, action):
        pass
    

class BoatMovementPhase(Phase):
        
    def __init__(self, game, **kwargs):
        super(BoatMovementPhase, self).__init__(game, **kwargs)
        
    def doAction(self, action):
        pass
    

class BoatAttackPhase(Phase):
    playersToAttack = ()
    playersResponded = ()
    currActions = ()
    weaponsAttacked = ()
    subphases = Enum('basubphases', ('poll', 'playerAttacks'))
    currPhase = None
    poll = None
    
    def __init__(self, game, **kwargs):
        super(BoatAttackPhase, self).__init__(game, **kwargs)
        self.playersToAttack = []
        self.playersResponded = []
        self.currActions = []
        self.weaponsAttacked = []
        self.currPhase = self.subphases.poll
        self.poll = IdleTimeoutPoll(self.game, 'Attack with your boats?',
                         ('yes', 'no'), 30.0)
        self.poll.poll()
        EventManager.registerCallback('poll.response', self.handlePollResponse)
        EventManager.registerCallback('poll.finish', self.pollFinished)
        
    def doAction(self, action):
        import combatActions
        if isinstance(action, combatActions.BoatCombatAction):
            if action.actors['weapon'] in self.weaponsAttacked:
                return
            self.weaponsAttacked.append(action.actors['weapon'])
            action.act()
        #Handle ending players' attack list
    
    def handlePollResponse(self, poll, player, resp):
        if poll == self.poll:
            self.playersResponded.append(player)
            if resp == 'yes' and player is not self.playersToAttack[-1]:
                self.playersToAttack.append(player)
    
    def pollFinished(self, poll, responses):
        #End the phase
        if poll == self.poll:
            game.phase = self.createNextPhase(self)


class MinifigAttackPhase(Phase):
    pass


class MinifigMovementPhase(Phase):
    pass


class OtherActionsPhase(Phase):
    pass


class AnimalActionsPhase(Phase):
    pass
            
# Do Phase registration
Phase.phases = (PreGamePhase, BoatMovementPhase, BoatAttackPhase,
                MinifigAttackPhase, MinifigMovementPhase, OtherActionsPhase,
                AnimalActionsPhase,
                postGame)